
import dao.*;
import entity.AddrInfo;
import entity.CommunityInfo;
import entity.OrderAll;
import entity.UserInfo;
import services.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) throws SQLException {
        while (true) {
            System.out.println("************************************");
            System.out.println("*                                  *");
            System.out.println("*                                  *");
            System.out.println("*                                  *");
            System.out.println("*        欢迎登录购物商城！！！！！     *");
            System.out.println("*                                  *");
            System.out.println("*                                  *");
            System.out.println("*                                  *");
            System.out.println("************************************");
            System.out.println("-------------------------------------");
            System.out.println("|1.还没有账号?立即注册 2.已有账号，立即登录|");
            System.out.println("-------------------------------------");
            Scanner scanner = new Scanner(System.in);
            int choose = Integer.valueOf(scanner.next());
            switch (choose) {
                case 1:
                    System.out.println("请输入用户名");
                    String userName2 = scanner.next();
                    System.out.println("请输入密码");
                    String userPassword2 = scanner.next();
                    System.out.println("请选择注册权限 1.普通用户 2.商品管理员");
                    int userRoot = Integer.valueOf(scanner.next());
                    System.out.println("请输入性别");
                    String userSex = scanner.next();
                    UserInfoDao.add(userName2, userPassword2, userRoot, userSex);
                    System.out.println("添加成功");
                    break;
                case 2:
                    int loginCount = 3;
                    while (true) {
                        System.out.println("请输入用户名");
                        String userName = scanner.next();
                        System.out.println("请输入密码");
                        String passWord = scanner.next();
                        UserInfo userInfo = new UserInfo(userName, passWord);
                        UserInfo result = UserInfoDao.Login(userInfo);

                        if (result == null) {
                            System.out.println("账号错误，请重新登录！！！");
                            loginCount--;
                            if (loginCount == 0) {
                                System.out.println("超出登录次数，即将退出系统！！！");
                                System.exit(1);
                            }
                            System.out.println("还剩" + loginCount + "次机会");
                            continue;
                        }

                        if (result != null) {
                            if (result.getDelStatus() == 0) {
                                System.out.println("账号错误，请重新登录！！！");
                                loginCount--;
                                continue;
                            } else {
                                Boolean secondFlag = true;
                                while (secondFlag) {
                                    int root = UserInfoDao.selectRoot(userName, passWord);
                                    if (root == 0) {
                                        System.out.println("欢迎您,请选择您要做的事情...");
                                        System.out.println("1.管理您信息 2.管理订单信息 3.管理收货地址  4.退出");
                                        int firstCho = Integer.valueOf(scanner.next());
                                        int secondColl = 0;
                                        Boolean firstFlag = true;
                                        switch (firstCho) {
                                            case 1:
                                                while (firstFlag) {
                                                    System.out.println("1.查看您的信息 2.修改您的信息 3.注销 4.返回上一步");
                                                    System.out.println("若要修改和注销，请先查询信息查找ID");
                                                    secondColl = Integer.valueOf(scanner.next());
                                                    switch (secondColl) {
                                                        case 1:
                                                            UserInfoServices.showUserInfo(UserInfoDao.secletInfo(userInfo));
                                                            break;
                                                        case 2:
                                                            System.out.println("请输入您的ID");
                                                            int id = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的姓名");
                                                            String newName = scanner.next();
                                                            System.out.println("请输入修改后的密码");
                                                            String newPassword = scanner.next();
                                                            System.out.println("请输入修改后的性别");
                                                            String newSex = scanner.next();
                                                            UserInfoDao.update(id, newName, newPassword, newSex);
                                                            break;
                                                        case 3:
                                                            UserInfoDao.delete(UserInfoDao.secletInfo(userInfo).getUserID(), 0);
                                                            System.out.println("用户已注销，即将退出系统");
                                                            System.exit(1);
                                                            break;
                                                        case 4:
                                                            firstFlag = false;
                                                            break;
                                                        default:
                                                            break;

                                                    }
                                                }
                                                break;
                                            case 2:
                                                boolean fifthFlag = true;
                                                while (fifthFlag) {
                                                    System.out.println("请选择您要做的事");
                                                    System.out.println("1.添加订单 2.查看您的订单信息 3.修改您的订单信息 4.删除订单 5.返回上一步");
                                                    int thirdCho = Integer.valueOf(scanner.nextInt());
                                                    switch (thirdCho) {
                                                        case 1:
                                                            Boolean thirdFlag = true;
                                                            while (thirdFlag) {
                                                                System.out.println("1.查看商品 2.购买商品，并下单 3.返回");
                                                                int fourthCho = Integer.valueOf(scanner.next());
                                                                switch (fourthCho) {
                                                                    case 1:
                                                                        CommunityServices.showCommunity();
                                                                        break;
                                                                    case 2:
                                                                        System.out.println("请输入商品ID");
                                                                        int comID = Integer.valueOf(scanner.next());
                                                                        System.out.println("请输入购买数量");
                                                                        Double buyCount = Double.valueOf(scanner.next());
                                                                        System.out.println("请输入收货ID");
                                                                        int addrID = Integer.valueOf(scanner.next());

                                                                        BuyCommunityServices.buyCommunity(UserInfoDao.secletInfo(userInfo).getUserID(), comID, addrID, buyCount);
                                                                        break;
                                                                    case 3:
                                                                        thirdFlag = false;
                                                                        break;
                                                                    default:
                                                                        break;
                                                                }
                                                            }
                                                            break;
                                                        case 2:
                                                            List<OrderAll> orderAllList = OrderInfoDao.selectOrderAll(UserInfoDao.secletInfo(userInfo).getUserID());
                                                            for (OrderAll orderAll : orderAllList) {
                                                                System.out.println("----------------------------------------------------------------------------------------------");
                                                                System.out.println("订单ID: " + orderAll.getOrderId() + "," + " 用户: " + orderAll.getUserName()
                                                                        + "," + " 商品: " + orderAll.getComName() + "," + " 商品价格: " + orderAll.getComPrice() + "," + " 数量: "
                                                                        + orderAll.getBuyCount() + "  收货地址: " + orderAll.getProvince() + " " + orderAll.getCity() + " "
                                                                        + orderAll.getArea() + " " + orderAll.getDetail()
                                                                );

                                                            }
                                                            System.out.println("----------------------------------------------------------------------------------------------");
                                                            break;
                                                        case 3:
                                                            System.out.println("请输入要修改的订单id");
                                                            int orderId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的商品id");
                                                            int comId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的地址id");
                                                            int addressId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的购买数量");
                                                            Double buyCount = Double.valueOf(scanner.next());
                                                            OrderService.changeOrder(orderId, comId, addressId, buyCount);

                                                            break;
                                                        case 4:
                                                            System.out.println("请输入要删除的订单id");
                                                            int orderId1 = Integer.valueOf(scanner.next());

                                                            OrderService.delete(orderId1);
                                                            break;

                                                        default:
                                                            fifthFlag = false;
                                                            break;
                                                    }
                                                }
                                                break;
                                            case 3:

                                                boolean sixthFlag = true;
                                                while (sixthFlag) {
                                                    System.out.println("1.查看所有收货地址 2.修改收货地址 3.删除收货地址 4.添加收货地址 5.返回上一步");
                                                    String thirdCommand = scanner.next();
                                                    switch (thirdCommand) {
                                                        case "1":
                                                            List<AddrInfo> addrInfoList = AddressDao.select(UserInfoDao.secletInfo(userInfo).getUserID());
                                                            for (AddrInfo addrInfo : addrInfoList) {
                                                                System.out.println("----------------------------------------------------------------------------------------------");
                                                                System.out.println("地址ID: " + addrInfo.getAddrID() + "," + " 用户ID: " + addrInfo.getUserID()
                                                                        + "  收货地址: " + addrInfo.getProvince() + " " + addrInfo.getCity() + " "
                                                                        + addrInfo.getArea() + " " + addrInfo.getDetail()
                                                                );

                                                            }
                                                            System.out.println("----------------------------------------------------------------------------------------------");

                                                            break;
                                                        case "2":
                                                            System.out.println("请输入要修改的地址ID");
                                                            int addrId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的省份");
                                                            String province = scanner.next();
                                                            System.out.println("请输入修改后的城市");
                                                            String city = scanner.next();
                                                            System.out.println("请输入修改后的区");
                                                            String area = scanner.next();
                                                            System.out.println("请输入修改后的地址详情");
                                                            String detail = scanner.next();
                                                            AddrInfo addrInfo = new AddrInfo(addrId, UserInfoDao.secletInfo(userInfo).getUserID(), province, city, area, detail);
                                                            AddressDao.update(addrInfo);
                                                            System.out.println("修改成功");

                                                            break;
                                                        case "3":
                                                            System.out.println("请输入要删除的地址ID");
                                                            int addrId1 = Integer.valueOf(scanner.next());
                                                            AddressDao.delete(addrId1);
                                                            System.out.println("删除成功");
                                                            break;
                                                        case "4":
                                                            System.out.println("请输入添加的省份");
                                                            String province1 = scanner.next();
                                                            System.out.println("请输入添加的城市");
                                                            String city1 = scanner.next();
                                                            System.out.println("请输入添加的区");
                                                            String area1 = scanner.next();
                                                            System.out.println("请输入添加的地址详情");
                                                            String detail1 = scanner.next();
                                                            AddrInfo addrInfo1 = new AddrInfo(UserInfoDao.secletInfo(userInfo).getUserID(), province1, city1, area1, detail1);
                                                            AddressDao.insert(addrInfo1);
                                                            System.out.println("添加成功");
                                                            break;
                                                        case "5":
                                                            sixthFlag = false;
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                                break;
                                            case 4:
                                                System.exit(1);
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                    if (root == 1) {
                                        System.out.println("欢迎您,商品管理员,请选择您要做的事情...");
                                        System.out.println("1.用户管理 2.商品管理 3.分类管理 4.查询订单 5.退出");
                                        int firstCho = Integer.valueOf(scanner.next());
                                        int secondColl = 0;
                                        switch (firstCho) {
                                            case 1:
                                                boolean eighthFlag = true;
                                                while (eighthFlag) {
                                                    System.out.println("1.查看您的信息 2.修改您的信息 3.注销 4.返回上一步");
                                                    System.out.println("若要修改和注销，请先查询信息查找ID");
                                                    secondColl = Integer.valueOf(scanner.next());
                                                    switch (secondColl) {
                                                        case 1:
                                                            UserInfoServices.showUserInfo(UserInfoDao.secletInfo(userInfo));
                                                            break;
                                                        case 2:
                                                            System.out.println("请输入您的ID");
                                                            int id = Integer.valueOf(scanner.next());

                                                            System.out.println("请输入修改后的姓名");
                                                            String newName = scanner.next();

                                                            System.out.println("请输入修改后的密码");
                                                            String newPassword = scanner.next();

                                                            System.out.println("请输入修改后的性别");
                                                            String newSex = scanner.next();
                                                            UserInfoDao.update(id, newName, newPassword, newSex);
                                                            break;
                                                        case 3:
                                                            System.out.println("请输入要注销的用户ID");
                                                            int delId = Integer.valueOf(scanner.next());
                                                            UserInfoDao.delete(delId, 0);
                                                            System.out.println("用户已注销，即将退出系统");
                                                            System.exit(1);
                                                            break;
                                                        case 4:
                                                            eighthFlag = false;
                                                            break;

                                                        default:
                                                            break;

                                                    }
                                                }
                                                break;
                                            case 2:
                                                boolean seventhFlag = true;
                                                while (seventhFlag) {
                                                    System.out.println("1.添加商品 2.查看商品信息 3.修改商品信息 4.删除商品 5.返回上一步");
                                                    int thirdCho = Integer.valueOf(scanner.next());
                                                    switch (thirdCho) {
                                                        case 1:
                                                            System.out.println("请输入商品类别ID");
                                                            int classifyId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入商品名称");
                                                            String comName = scanner.next();
                                                            System.out.println("请输入商品价格");
                                                            Double comPrice = Double.valueOf(scanner.next());
                                                            System.out.println("请输入商品数量");
                                                            Double comCount = Double.valueOf(scanner.next());
                                                            CommunityInfo communityInfo = new CommunityInfo(classifyId, comName, comPrice, comCount);
                                                            CommunityDao.insert(communityInfo);
                                                            System.out.println("添加成功");
                                                            break;
                                                        case 2:
                                                            CommunityServices.showCommunity();
                                                            break;
                                                        case 3:
                                                            System.out.println("请输入要修改的商品ID");
                                                            int comId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的商品类别ID");
                                                            int classifyId1 = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的商品名称");
                                                            String comName1 = scanner.next();
                                                            System.out.println("请输入修改后的商品价格");
                                                            Double comPrice1 = Double.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的商品数量");
                                                            Double comCount1 = Double.valueOf(scanner.next());
                                                            CommunityInfo communityInfo1 = new CommunityInfo(comId, classifyId1, comName1, comPrice1, comCount1);
                                                            CommunityDao.update(communityInfo1);
                                                            System.out.println("修改成功");
                                                            break;
                                                        case 4:
                                                            System.out.println("请输入要删除的商品ID");
                                                            int comId1 = Integer.valueOf(scanner.next());
                                                            CommunityDao.delete(comId1);
                                                            System.out.println("删除成功");
                                                            break;
                                                        case 5:
                                                            seventhFlag = false;
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                                break;
                                            case 3:
                                                boolean flag = true;
                                                while (flag) {
                                                    System.out.println("1.添加分类 2.修改分类 3.查询分类 4.返回上一步");
                                                    int command = Integer.valueOf(scanner.next());
                                                    switch (command) {
                                                        case 1:
                                                            System.out.println("请输入要添加的分类名称");
                                                            String className = scanner.next();
                                                            ClassifyDao.insert(className);
                                                            System.out.println("添加成功");
                                                            break;
                                                        case 2:
                                                            System.out.println("请输入要修改的分类ID");
                                                            int classId = Integer.valueOf(scanner.next());
                                                            System.out.println("请输入修改后的分类名称");
                                                            String className1 = scanner.next();
                                                            ClassifyDao.update(classId, className1);
                                                            System.out.println("修改成功");
                                                            break;
                                                        case 3:
                                                            ClassifyServices.showClassify();
                                                            break;
                                                        case 4:
                                                            flag = false;
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                                break;
                                            case 4:
                                                List<OrderAll> orderAllList = OrderInfoDao.selectOrderAll();
                                                for (OrderAll orderAll : orderAllList) {
                                                    System.out.println("----------------------------------------------------------------------------------------------");
                                                    System.out.println("订单ID: " + orderAll.getOrderId() + "," + " 用户: " + orderAll.getUserName()
                                                            + "," + " 商品: " + orderAll.getComName() + "," + " 商品价格: " + orderAll.getComPrice() + "," + " 数量: "
                                                            + orderAll.getBuyCount() + "  收货地址: " + orderAll.getProvince() + " " + orderAll.getCity() + " "
                                                            + orderAll.getArea() + " " + orderAll.getDetail()
                                                    );

                                                }
                                                System.out.println("----------------------------------------------------------------------------------------------");

                                                break;
                                            case 5:
                                                System.exit(1);
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                default:
                    break;
            }


        }
    }
}
